#include "Os.h"
#include "EcuM.h"

#include "Mcu.h"
#include "Port.h"
#include "Dio.h"
#include "Gpt.h"
#include "Adc.h"
#include "Pwm.h"

void delay(int cycles)
{
  while(cycles--);
}

void Dio_Test()
{
  Dio_WriteChannel(DioConf_DioChannel_DioChannel_PTD15_LED_RED,0);
  Dio_WriteChannel(DioConf_DioChannel_DioChannel_PTD15_LED_RED,1);
}

void Gpt_Test()
{
  static uint8 flag = 0;
  uint32 elapsedTimer = Gpt_GetTimeElapsed(GptConf_GptChannelConfiguration_GptChannelConfiguration_0);
  Gpt_StartTimer(GptConf_GptChannelConfiguration_GptChannelConfiguration_0,240000);
  if(elapsedTimer == 0)
  {
    if(flag == 0)
    {
      Dio_WriteChannel(DioConf_DioChannel_DioChannel_PTD15_LED_RED,1);
      flag = 1;
    }
    else
    {
      Dio_WriteChannel(DioConf_DioChannel_DioChannel_PTD15_LED_RED,0);
      flag = 0;
    }
  }
}

void Adc_Test()
{
  static uint16 data=0;
  delay(200);
  Adc_StartGroupConversion(AdcGroup_SW);
  delay(200);
  Adc_ReadGroup(AdcGroup_SW,&data);
  delay(200);
  Adc_StopGroupConversion(AdcGroup_SW);
  delay(200);
}

void Pwm_Test()
{
  static boolean increaseDuty = TRUE;
  static uint32 duty = 0;
  Pwm_SetDutyCycle(PwmConf_PwmChannelConfigSet_PwmChannel_PTD16_FTM0_CH1,duty);
  if (increaseDuty == TRUE)
     {
        duty++;
        if (duty > 0x7FFFU)
            increaseDuty = FALSE;
     }
  else
     {
        duty--;
        if (duty < 1U)
            increaseDuty = TRUE;
     }
}

int main()
{
  Mcu_Init( &McuModuleConfiguration );
  Mcu_InitClock(McuConf_McuClockSettingConfig_McuClockSettingConfig_0);
  while (MCU_PLL_LOCKED != Mcu_GetPllStatus());
  Mcu_DistributePllClock();
  Port_Init( &PortConfigSet );
  
  Gpt_Init( &GptChannelConfigSet );
  Adc_Init( &AdcConfigSet );

  Pwm_Init(&PwmChannelConfigSet);

  while (1)
  {

  }

}
